package cn.qingyun.gis.modules.init.system.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
public class InterceptorConfigure implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 处理H5的要求，个性化的异常状态处理
        registry.addInterceptor(new HandlerInterceptorAdapter() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

                Object exception = request.getAttribute("javax.servlet.error.exception");
                if (exception != null) {
                    if (exception.toString().indexOf("org.apache.shiro.authc.AuthenticationException") != -1) {
                        request.setAttribute("javax.servlet.error.status_code", HttpStatus.UNAUTHORIZED.value());
                    }
                }
                return true;
            }

        }).addPathPatterns("/error");

    }
}
